package service.impl;

import mapper.BookMapper;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Book;
import pojo.User;
import pojo.vo.UserVo;
import service.UserService;
import utils.GetSqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-12-06
 * Time: 11:14
 */
public class UserServiceImpl implements UserService {
    SqlSession sqlSession = GetSqlSession.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    @Override
    public User login(String accout, String password) {
        User user = null;
        if( accout==null || accout == ""  || password ==null || password =="") {
            return null;
        }
        //判断账号是否存在
        user = userMapper.login(accout, password);
        if( user == null) return null;
        //判断密码是否正确
        if( user.getPassword().equals(password)){
            return user;
        } else {
            return null;
        }
    }

    /**
     * 根据账号获取用户对象
     */
    @Override
    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }

    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }

    /**
     * 修改用户信息
     */
    @Override
    public boolean modifyUser(User user) {
        return userMapper.modifyUser(user);
    }

    public User Search(String account)
    {
        return userMapper.Search(account);
    }

    /**
     * 注册
     * @param account
     * @param password
     * @param nick_name
     * @param category_id
     * @return
     */
    public boolean register(String account,String password,String nick_name,long category_id)
    {
        if(userMapper.Search(account)==null){
            return userMapper.register(account,password,nick_name,category_id);
        }
        else return false;
    }


    @Override
    public boolean addUser(String account, String password, String nick_name, Long category_id, Long already_number) {
//        List<User> users = userMapper.listUser();
//        if(users.size()>0) return false;
        return userMapper.addUser(account,password,nick_name,category_id,already_number);
    }

    @Override
    public boolean deleteUser1(String account) {
        return userMapper.deleteUser1(account);
    }

    @Override
    public List<UserVo> listUser() {
        return userMapper.listUser();
    }

    @Override
    public boolean updateUser(User newuser) {
        return userMapper.updateUser(newuser);
    }

    @Override
    public List<User> listuserByAccount(String account) {
        return userMapper.listuserByAccount(account);
    }
}
